package com.zf.zffencheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zf.zffencheng.dao.TenantDao;
import com.zf.zffencheng.model.dto.TenantFilter;
import com.zf.zffencheng.model.pojo.Tenant;
import com.zf.zffencheng.model.vo.TenantVo;
import com.zf.zffencheng.service.TenantService;
import com.zf.zffencheng.util.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: TenantServiceImpl <br/>
 * Description:  <br/>
 * date: 2021/5/31 13:59<br/>
 *
 * @author william <br/>
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantDao, Tenant> implements TenantService {
    @Override
    public Page<TenantVo> pageFilter(TenantFilter filter) {
        Page<Tenant> offset = new Page<>(filter.getPage(), filter.getPageSize());
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
        if(filter != null){
            if(StringUtils.hasLength(filter.getTenantName())){
                queryWrapper.like("TenantName", filter.getTenantName());
            }
        }
        queryWrapper.orderByDesc("CreateTime");
        Page<Tenant> page = page(offset, queryWrapper);
        List<Tenant> records = page.getRecords();
        List<TenantVo> collect = records.stream().map(val -> BeanUtils.copyAs(val, TenantVo.class))
                .collect(Collectors.toList());
        Page<TenantVo> tenantVoPage = new Page<>();
        return tenantVoPage.setTotal(page.getTotal()).setRecords(collect);
    }
}
